{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from oemof import solph\n",
    "\n",
    "import pickle\n",
    "import os\n",
    "import pandas as pd\n",
    "import matplotlib.pyplot as plt\n",
    "from matplotlib.dates import DateFormatter, YearLocator, MonthLocator\n",
    "import matplotlib\n",
    "import oemof_visio as oev\n",
    "%matplotlib inline\n",
    "matplotlib.rcParams['figure.figsize'] = [20.0, 7.0]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Restore the whole energysystem with results"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "energysystem = solph.EnergySystem()\n",
    "energysystem.restore(dpath=None, filename=None)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Convert keys to strings and print all keys"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "string_results = solph.views.convert_keys_to_strings(energysystem.results['main'])\n",
    "print(string_results.keys())"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Use the outputlib to collect all the flows into and out of the electricity bus\n",
    "Collect all flows into and out of the electricity bus by using solph.views.node()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "node_results_bel = solph.views.node(energysystem.results['main'], 'bel')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "What we will be working with now is a pandas dataframe. Have a look at these links to learn about pandas, especially the last one (pandas in 10min):  \n",
    "https://pandas.pydata.org/  \n",
    "http://pandas.pydata.org/pandas-docs/stable/  \n",
    "http://pandas.pydata.org/pandas-docs/stable/10min.html"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "df = node_results_bel['sequences']\n",
    "df.head(2)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Use pandas functionality to create a plot of all the columns of the dataframe"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "ax = df.plot(kind='line', drawstyle='steps-post')\n",
    "ax.set_xlabel('Time [h]')\n",
    "ax.set_ylabel('Energy [MWh]')\n",
    "ax.set_title('Flows into and out of bel')\n",
    "ax.legend(loc='center left', bbox_to_anchor=(1.0, 0.5)) # place legend outside of plot\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### oemof visio provides a function that collects the column names for in and outflows as lists in a dictionary"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "in_out_dictionary = oev.plot.divide_bus_columns('bel', df.columns)\n",
    "in_cols = in_out_dictionary['in_cols']\n",
    "out_cols = in_out_dictionary['out_cols']"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "This allows us to get the all the columns that are outflows:"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We can get any column of the dataframe by providing its label as a list"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "bel_to_demand_el = [(('bel', 'demand_el'), 'flow')] #  this is a list with one element\n",
    "df[bel_to_demand_el].head(2)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Plot only outflows"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "ax = df[out_cols].plot(kind='line', drawstyle='steps-post')\n",
    "ax.set_xlabel('Time [h]')\n",
    "ax.set_ylabel('Energy [MWh]')\n",
    "ax.set_title('Flows into or out of bel')\n",
    "ax.legend(loc='center left', bbox_to_anchor=(1.0, 0.5)) # place legend outside of plot\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Use the functions of oemof_visio to create plots\n",
    "See also: oemof_examples/examples/oemof_0.2/plotting_examples/storage_investment_plot.py\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Use color palette generators to generate a suitable color list, e.g.:     \n",
    "http://javier.xyz/cohesive-colors/  \n",
    "https://colourco.de/  \n",
    "http://seaborn.pydata.org/tutorial/color_palettes.html  "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "inorder = [(('pp_chp', 'bel'), 'flow'),\n",
    "             (('pp_coal', 'bel'), 'flow'),\n",
    "             (('pp_gas', 'bel'), 'flow'),\n",
    "             (('pp_lig', 'bel'), 'flow'),\n",
    "             (('pp_oil', 'bel'), 'flow'),\n",
    "             (('pv', 'bel'), 'flow'),\n",
    "             (('wind', 'bel'), 'flow')]\n",
    "\n",
    "outorder = [(('bel', 'demand_el'), 'flow'),\n",
    "             (('bel', 'excess_el'), 'flow'),\n",
    "             (('bel', 'heat_pump'), 'flow')]\n",
    "\n",
    "cdict = {(('pp_chp', 'bel'), 'flow'): '#eeac7e',\n",
    "        (('pp_coal', 'bel'), 'flow'): '#0f2e2e',\n",
    "        (('pp_gas', 'bel'), 'flow'): '#c76c56',\n",
    "        (('pp_lig', 'bel'), 'flow'): '#56201d',\n",
    "        (('pp_oil', 'bel'), 'flow'): '#494a19',\n",
    "        (('pv', 'bel'), 'flow'): '#ffde32',\n",
    "        (('wind', 'bel'), 'flow'): '#4ca7c3',\n",
    "        (('bel', 'demand_el'), 'flow'): '#ce4aff',\n",
    "        (('bel', 'excess_el'), 'flow'): '#555555',\n",
    "        (('bel', 'heat_pump'), 'flow'): '#42c77a'}\n",
    "\n",
    "\n",
    "fig = plt.figure(figsize=(13, 5))\n",
    "\n",
    "my_plot = oev.plot.io_plot('bel', df,\n",
    "                           inorder=inorder,\n",
    "                           outorder=outorder,\n",
    "                           cdict=cdict,\n",
    "                           ax=fig.add_subplot(1, 1, 1),\n",
    "                           smooth=False)\n",
    "ax = my_plot['ax']\n",
    "oev.plot.set_datetime_ticks(ax, df.index, tick_distance=32,\n",
    "                            date_format='%d-%m-%H', offset=12)\n",
    "\n",
    "my_plot['ax'].set_ylabel('Power in MW')\n",
    "my_plot['ax'].set_xlabel('2012')\n",
    "my_plot['ax'].set_title(\"Electricity bus\")\n",
    "legend = ax.legend(loc='center left', bbox_to_anchor=(1.0, 0.5)) # place legend outside of plot\n",
    "\n",
    "# save figure\n",
    "fig = ax.get_figure()\n",
    "fig.savefig('myplot.png', bbox_inches='tight')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
